package GUI.Lesson;

import java.awt.*;
import java.awt.event.*;

public class TextCalculatorBest {
    public static void main(String[] args) {
        new Calculatorss("计算器").loadFrame();
    }
}


//优化后的面向对象
//计算机类
class Calculatorss extends Frame {
    public Calculatorss(String title) {
        super(title);
    }

    TextField textField1,textField2,textField3;
    public void loadFrame() {
        //三个文本框
        textField1 = new TextField(10);
        textField2 = new TextField(10);
        textField3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new Actionss());

        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        pack();
        setVisible(true);
    }
    //监听类
    private class Actionss implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int n1 = Integer.parseInt(textField1.getText());
            int n2 = Integer.parseInt(textField2.getText());

            //2.将这个值加法运算后赋值给第三个文本框
            textField3.setText(""+(n1+n2));

            //清楚前两个文本框
            textField1.setText("");
            textField2.setText("");
        }
    }

}

